const express = require('express');

const app = express();

app.use((req, res, next) => {
    if(req.headers['content-type'] === 'application/json') {
        req.on('data', (data) => {
            console.log(data.toString());
            const info = JSON.parse(data.toString());
            req.body = info;
            
        })
    
        req.on('end', () => {
           
            next(); 
        })
    } else {
        next();
    }
})

// body-parser express3.x 内置于express框架
// body-parser express4.x 被分离出去
// body-parser express4.16.x 内置成函数 

// app.use(express.json());

// true: 那么对urlencoded进行解析时,它使用的是第三方库qs
// false: 那么对urlencoded进行解析时,它使用的是Node内置模块
// app.use(express.urlencoded({extended: true}));

app.post('/login', (req, res, next) => {
   console.log(req.body);
   res.end('leon welcome back')
})

app.post('/products', (req, res, next) => {
    console.log(req.body);
    res.end('upload Products info')
})
app.listen(8000, () => {
    console.log('express服务器启动成功');
})